package com.lagou.mapper;

import com.lagou.DataSourceUtil;
import com.lagou.connect.DefaultConectionManager;
import com.lagou.connect.TransactionIsolationLevel;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author: wudong
 * @create: 2021-05-19 14:02
 **/
public class SpringConectionManager extends DefaultConectionManager {
    private boolean springManager;

    public SpringConectionManager(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit) {
        super(dataSource,level,autoCommit);
    }


    @Override
    public Connection getConnection() throws SQLException {
        DataSourceUtil.ConnectionHolder connectionHolder = DataSourceUtil.getConnect(this.dataSource);
        if(connectionHolder != null){
            springManager = true;
            return connectionHolder.getConect();
        }
        return super.getConnection();
    }

    @Override
    public void commit() throws SQLException {
        if(!springManager){
            super.commit();
        }
    }

    @Override
    public void rollback() throws SQLException {
        if(!springManager){
            super.rollback();
        }
    }

    @Override
    public void close() throws SQLException {
        if(!springManager){
            super.close();
        }
    }
}
